/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
*/

package scm;
import java.sql.*;
import javax.swing.*;
import java.util.ArrayList;


/**
 *
 * @author Łukasz Synówka
 */
public class Akcje extends JFrame {

    private Connection c = null;
    DefaultListModel listModel = new DefaultListModel();
    private ArrayList<Integer> actionIds;

    /** Creates new form DodawanieProcedury */
    public Akcje() {
        initComponents();
        wypelnij_jComboBox();        
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        this.setVisible(true);
    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();        
        jButtonEdit = new javax.swing.JButton();
        jButtonAdd = new javax.swing.JButton();
        jButtonRemove = new javax.swing.JButton();
        jComboBox = new javax.swing.JComboBox();        
        

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setName("Form"); // NOI18N


        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jButtonAdd.setText("Dodaj"); // NOI18N
        jButtonAdd.setName("jButtonAdd"); // NOI18N
        jButtonAdd.addMouseListener(new java.awt.event.MouseAdapter()
        {
            public void mouseClicked(java.awt.event.MouseEvent evt)
            {
                jButtonAddMouseClicked(evt);
            }
        });

        jButtonEdit.setText("Modyfikuj"); // NOI18N
        jButtonEdit.setName("jButtonEdit"); // NOI18N
        jButtonEdit.addMouseListener(new java.awt.event.MouseAdapter()
        {
            public void mouseClicked(java.awt.event.MouseEvent evt)
            {
                jButtonEditMouseClicked(evt);
            }
        });

        jButtonRemove.setText("Usuń"); // NOI18N
        jButtonRemove.setName("jButtonRemove"); // NOI18N
        jButtonRemove.addMouseListener(new java.awt.event.MouseAdapter()
        {
            public void mouseClicked(java.awt.event.MouseEvent evt)
            {
                jButtonRemoveMouseClicked(evt);
            }
        });      

        jComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { }));
        jComboBox.setName("jComboBox"); // NOI18N


        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jComboBox, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(jButtonAdd)
                        .addGap(18, 18, 18)
                        .addComponent(jButtonEdit)
                        .addGap(18,18,18)
                        .addComponent(jButtonRemove)
                        ))
                .addContainerGap(21, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addComponent(jComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonAdd)
                    .addComponent(jButtonEdit)
                    .addComponent(jButtonRemove)
                    )
                .addContainerGap(21, Short.MAX_VALUE))
        );
      
        pack();

        Akcja a = new Akcja();
        a.wykonaj(4);       
    }// </editor-fold>

    

    private void jButtonAddMouseClicked(java.awt.event.MouseEvent evt)
    {
        DodajAkcje da = new DodajAkcje(this);        
    }

    private void jButtonEditMouseClicked(java.awt.event.MouseEvent evt)
    {
        int actionId = actionIds.get(jComboBox.getSelectedIndex());        
        EdytujAkcje ea = new EdytujAkcje(this, actionId);
    }

    private void jButtonRemoveMouseClicked(java.awt.event.MouseEvent evt)
    {
        int actionId = actionIds.get(jComboBox.getSelectedIndex());

        int response = JOptionPane.showConfirmDialog(null, "Czy na pewno usunąć wybraną akcje?");
        if (response == JOptionPane.YES_OPTION)
        {
            try
            {
                Class.forName("org.postgresql.Driver");
            }
            catch (ClassNotFoundException cnfe)
            {
                JOptionPane.showMessageDialog(null, cnfe.getMessage());
            }
            try
            {
                c = DriverManager.getConnection("jdbc:postgresql://localhost/template1",
                                        "postgres", "przemek");

                PreparedStatement ps = c.prepareStatement("SELECT 1 FROM proc_act WHERE id_act = ?");
                ps.setInt(1, actionId);
                ResultSet rs = ps.executeQuery();

                if (rs.next())
                {
                    JLabel errorFields = new JLabel("<HTML><FONT COLOR = Blue>Nie możesz usunąć akcji dopóki jest ona używana w procedurach.</FONT></HTML>");
                    JOptionPane.showMessageDialog(null,errorFields);
                    this.setVisible(true);
                }
                else
                {
                    ps = c.prepareStatement("DELETE FROM actions WHERE id = ?");
                    ps.setInt(1, actionId);
                    ps.executeUpdate();
                    
                    JLabel errorFields = new JLabel("<HTML><FONT COLOR = Blue>Akcja została usunięta.</FONT></HTML>");
                    JOptionPane.showMessageDialog(null,errorFields);
                    this.setVisible(true);
                    wypelnij_jComboBox();


                }
            }
            catch (SQLException se)
            {
                JOptionPane.showMessageDialog(null, se.getMessage());
            }
        }         
    }
    
    public void wypelnij_jComboBox()
    {
        try
        {
            Class.forName("org.postgresql.Driver");
        }
        catch (ClassNotFoundException cnfe)
        {
            JOptionPane.showMessageDialog(null, cnfe.getMessage());
        }
        try
        {
            c = DriverManager.getConnection("jdbc:postgresql://localhost/template1",
                                    "postgres", "przemek");

            PreparedStatement ps = c.prepareStatement("SELECT id, name FROM actions ORDER BY id");
            ResultSet rs = ps.executeQuery();
            jComboBox.removeAllItems();
            actionIds = new java.util.ArrayList<Integer>();
            while (rs.next())
            {
                actionIds.add(Integer.parseInt(rs.getString("id")));
                jComboBox.addItem(rs.getString("id")+"."+rs.getString("name"));

            }
        }
        catch (SQLException se)
        {
            JOptionPane.showMessageDialog(null, se.getMessage());
        }
    }

    // Variables declaration - do not modify
    private javax.swing.JButton jButtonAdd;
    private javax.swing.JButton jButtonEdit;
    private javax.swing.JButton jButtonRemove;
    private javax.swing.JComboBox jComboBox;        
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration
}

